package com.example.HelloSpace.adapters;

import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.HelloSpace.models.MissionInfoModel;
import com.example.HelloSpace.ui.MissionInfoView;
import com.example.HelloSpace.viewmodels.MissionListViewModel;

import java.util.List;


public class MissionInfoAdapter extends RecyclerView.Adapter<MissionInfoAdapter.MissionInfoViewHolder> {

    private List<MissionInfoModel> missionInfoList;
    private MissionListViewModel missionListViewModel;

    public MissionInfoAdapter(List<MissionInfoModel> missionInfoList) {
        this.missionInfoList = missionInfoList;
    }
    @NonNull
    @Override
    public MissionInfoViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        MissionInfoView view = new MissionInfoView(parent.getContext());
        return new MissionInfoViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MissionInfoViewHolder holder, int position) {
        MissionInfoModel missionInfo = missionInfoList.get(position);
        if (missionInfo != null) {
            holder.missionInfoView.setMissionInfo(missionInfo);
            holder.missionInfoView.setMissionListViewModel(missionListViewModel);
        } else {
            Log.e("MissionInfoAdapter", "MissionInfoModel at position " + position + " is null");
        }
    }

    static class MissionInfoViewHolder extends RecyclerView.ViewHolder {
        MissionInfoView missionInfoView;

        MissionInfoViewHolder(View itemView) {
            super(itemView);
            missionInfoView = (MissionInfoView) itemView;
        }
    }

    public void updateMissionInfoList(List<MissionInfoModel> missionInfoList) {
        this.missionInfoList = missionInfoList;
        notifyDataSetChanged();
    }
    public void setMissionListViewModel(MissionListViewModel missionListViewModel) {
        this.missionListViewModel = missionListViewModel;
    }

    @Override
    public int getItemCount() {
        return missionInfoList == null ? 0 : missionInfoList.size();
    }

}
